<template>
  <div class="content-page">
    <img :src="imgHost+imgArr[currentPage - 1]" alt="" />
    <div class="pagination">
      <div class="pre" v-show="currentPage !== 1" @click="handlePre">
        <i class="iconfont icon-you"></i>
      </div>
      <div class="pre-off" v-show="currentPage !== 1" @click="handlePre">
        <i class="iconfont icon-you"></i>上一页
      </div>
      <div
        class="page-num"
        v-for="item in numList"
        :key="item"
        @click="handlePage(item)"
        :style="
          currentPage == item ? { background: '#0088cc', color: '#fff' } : {}
        "
      >
        {{ item }}
      </div>
      <div class="next" v-show="currentPage !== totalPage" @click="handledNext">
        <i class="iconfont icon-zuo"></i>
      </div>
      <div
        class="next-off"
        v-show="currentPage !== totalPage"
        @click="handledNext"
      >
        下一页<i class="iconfont icon-zuo"></i>
      </div>
      <div class="input-page">
        <input
          type="text"
          placeholder="页码"
          v-model="goPage"
          @keydown.enter="handleGoPage"
        />
        <i @click="handleGoPage" class="iconfont icon-youjiantou"></i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ContentPage",
  props:{
    imgHost:String,
    imgArr:Array
  },
  data() {
    return {
      currentPage: 1, //当前页码
      totalPage: 1, //总页数
      numList: [], //页码列表
      articleList: [], //数据列表
      goPage: "",
    };
  },
  created() {
    this.setPageNum();
  },
  methods: {
    setPageNum() {
      document.querySelector("html").scrollTop=0;
      // console.log(this.imgArr);
      this.numList = [];
      this.totalPage = this.imgArr.length;
      if (this.totalPage > 10 && this.currentPage > 6) {
        if (this.currentPage == this.totalPage) {
          for (let i = this.currentPage - 10; i < this.currentPage; i++) {
            this.numList.push(i + 1);
          }
        } else if (this.currentPage + 1 == this.totalPage) {
          for (let i = this.currentPage - 9; i < this.currentPage + 1; i++) {
            this.numList.push(i + 1);
          }
        } else if (this.currentPage + 2 == this.totalPage) {
          for (let i = this.currentPage - 8; i < this.currentPage + 2; i++) {
            this.numList.push(i + 1);
          }
        } else if (this.currentPage + 3 == this.totalPage) {
          for (let i = this.currentPage - 7; i < this.currentPage + 3; i++) {
            this.numList.push(i + 1);
          }
        } else if (this.currentPage + 4 == this.totalPage) {
          for (let i = this.currentPage - 6; i < this.currentPage + 4; i++) {
            this.numList.push(i + 1);
          }
        } else {
          for (let i = this.currentPage - 5; i < this.currentPage + 5; i++) {
            this.numList.push(i + 1);
          }
        }
      } else {
        for (let i = 0; i < this.totalPage; i++) {
          if (i > 9) break;
          this.numList.push(i + 1);
        }
      }
    },
    handlePre() {
      this.currentPage--;
      this.setPageNum();
    },
    handledNext() {
      this.currentPage++;
      this.setPageNum();
    },
    handlePage(page) {
      this.currentPage = page;
      this.setPageNum();
    },
    handleGoPage() {
      // console.log(typeof this.goPage);
      let page = parseInt(this.goPage);
      if (typeof page != "number") {
        return;
      }
      if (page < 1 || page > this.totalPage) {
        alert("超出范围");
        return;
      }
      this.currentPage = page;
      this.setPageNum();
      this.goPage = "";
    },
  },
};
</script>

<style lang="less" scoped>
@media only screen and (min-width: 768px) {
  .pre-off {
    display: none;
  }
  .next-off {
    display: none;
  }
}
@media only screen and (max-width: 768px) {
  .pre {
    display: none;
  }
  .page-num {
    display: none;
  }
  .next {
    display: none;
  }
  .input-page {
    display: none !important;
  }
}
.content-page {
  img {
    width: 100%;
  }
  .pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    color: rgba(56, 56, 56, 0.8);
    text-align: center;
    font-size: 13px;
    padding: 20px 0;
    span {
      height: 28px;
      line-height: 28px;
      border-radius: 2px;
      padding: 0 8px;
      margin: 0 5px;
      background: #f4f4f5;
    }
    div {
      min-width: 30px;
      height: 28px;
      line-height: 28px;
      box-sizing: border-box;
      border-radius: 2px;
      padding: 0 8px;
      margin: 0 5px;
      background: #f4f4f5;

      cursor: pointer;
      &:hover {
        color: #fff;
        background: #3361ea;
      }
    }
    .pre-off,
    .next-off {
      .iconfont {
        vertical-align: middle;
      }
    }
    .input-page {
      position: relative;
      width: 66px;
      height: 28px;
      padding: 0;
      display: flex;
      align-items: center;
      input {
        width: 66px;
        height: 28px;
        padding-left: 10px;
        border: 1px solid #eee;
        box-sizing: border-box;
        outline: none;
      }
      i {
        width: 28px;
        position: absolute;
        top: 0;
        right: 0;
        padding: 0 5px;
        box-sizing: border-box;
        color: #333;
        &:hover {
          color: #0088cc;
        }
      }
    }
  }
}
</style>